<?php
/**
*
* 功能描述（留言控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class CtrlLvmsg extends CtrlBase
{
	public $paramCheck = array(
		"showlist" => array(
			'status' => array("GET", "0", "intval", "notnegative"),
			'type' => array("GET", "0", "intval", "notnegative"),
			"page" => array("GET", "1", "intval", "positive"),
			'key' => array("GET", "", "trim", ""),
			'keyword' => array("GET", "", "trim", ""),
		),
		"setstatus" => array(
			'id' => array("GET", "must", "intval", "positive"),
			'status' => array("GET", "must", "intval", "positive"),
		),
		"remove" => array(
			'id' => array("GET", "must", "intval", "positive"),
		),
		"down" => array(
			"file" => array("GET", "must", "trim", "notempty"),
		),
	);

	public $perm = array('msg', "index.php?action=login.tologin");

	function ShowList()
	{
		$key = $this->params['key'];
		$keyword = $this->params['keyword'];
		$type = $this->params['type'];
	   	$status = $this->params['status'];
		$page = $this->params['page'];
		$obj = $this->GetModel("lvmsg");
		$data = $obj->GetList($type, $status, $page, $key, $keyword);
		$this->assign("msgs", $data['data']);
		$this->assign("totalCnt", $data['cnt']);
		$this->assign("pageCnt", $data['pageCnt']);
		$this->assign("curPage", $page);
		$this->assign("type", $type);
		$this->assign("status", $status);
		$this->assign('key', $key);
		$this->assign('keyword', $keyword);
		return RT_TPL;
	}

	function SetStatus()
	{
		$id = $this->params['id'];
		$sta = $this->params['status'];
		$data = array(
			'status' => $sta,
		);
		$obj = $this->GetModel("lvmsg");
		$cnt = $obj->UpdateMsg($data, $id);
		if($cnt == 0)
			echo "2";
		else
		{
			$this->CoreLog('msg_sta', $id, $sta, '');
			echo "0";
		}
		return RT_NONE;
	}

	function Remove()
	{
		$obj = $this->GetModel("lvmsg");
		$id = $this->params['id'];
		$attach = $obj->GetAttach($id);
		if($attach === false)
		{
			echo "3";
			return RT_NONE;
		}
		else if(!empty($attach))
		{
			$msg = "";
			switch(RmFile($attach, $msg))
			{
			case 0:
				$this->CoreLog('file_rm', $id, 1, $attach);
				break;
			case -1: 
				break;
			case -2:
				echo $msg;
				return RT_NONE;
				break;
			}
		}
		$cnt = $obj->RemoveMsg($id);
		if($cnt == 0)
			echo "3";
		else
		{
			echo "0";
			$this->CoreLog('msg_rm', $id, 0, '');
		}
		return RT_NONE;
	}

	function Down()
	{
		$file = $this->params['file'];
		$msg = "";
		if(SafeDownFile($file, $msg) < 0)
		{
			$this->assign("msg", $msg);
			return RT_MSG;
		}
		return RT_NONE;
	}
}

